import subprocess

cisco_oid = {
    "TsLineStatus":"1.3.6.1.4.1.9.2.9.2.1.1",
    "TsLineType":"1.3.6.1.4.1.9.2.9.2.1.2",
    "TsLineClear":"1.3.6.1.4.1.9.2.9.10.0",
}

class SnmpAgent():
    def __init__(self):
        '''
        Find snmpget/snmpwalk/snmpset path
        '''
        self.snmpgetpath = subprocess.check_output(["which","snmpget"]).strip()
        self.snmpwalkpath = subprocess.check_output(["which","snmpwalk"]).strip()
        self.snmpsetpath = subprocess.check_output(["which","snmpset"]).strip()

    def parse(self,data):
        return [(line.split('=')[0].strip(),line.split('=')[1].strip().split(':')[0],line.split('=')[1].strip().split(':')[1].strip()) for line in data.strip().split('\n')]

    def get(self,version,host,community,oid):
        """
        Executes snmpget -v x -c community host oid
        and returns result
        """
        try:
            return self.parse(subprocess.check_output([self.snmpgetpath,"-v",version,"-c",community,host,oid]))
        except:
            return False

    def walk(self,version,host,community,oid):
        """
        Executes snmpwalk -v x -c community host oid
        and returns result
        """
        try:
            return self.parse(subprocess.check_output([self.snmpwalkpath,"-v",version,"-c",community,host,oid]))
        except:
            return False

    def set(self,version,host,community,oid,type,value):
        """
        Executes snmpset -v x -c community oid type value
        and returns result
        """
        try:
            return subprocess.check_output([self.snmpsetpath,"-v",version,"-c",community,host,oid,type,str(value)])
        except:
            return False
